Skip to content

Warn at runtime on degree coords + meter elevation in slope planar path#2777

Merged
brendancol merged 2 commits into
mainfrom
issue-2764
Jun 2, 2026
Merged

Warn at runtime on degree coords + meter elevation in slope planar path#2777
brendancol merged 2 commits into
mainfrom
issue-2764

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Closes #2764

The planar slope path uses coordinate spacing directly as the cell size. When coordinates are in degrees (lat/lon) but elevation is in meters, the rise-over-run mixes units and the output is wrong by orders of magnitude, silently. A warning helper for this case already existed (warn_if_unit_mismatch in utils.py) but nothing called it.

  • Wire warn_if_unit_mismatch into the planar branch of slope() so a UserWarning fires when degree-like coordinates meet meter-like elevation.
  • Document the runtime warning in the slope docstring.

No new computation, only a runtime check on the existing planar path, so all four backends (numpy / cupy / dask+numpy / dask+cupy) are unaffected. The warning runs on the eager DataArray metadata before dispatch.

Test plan:

  • New test asserts the warning fires for degree coordinates with meter elevation.
  • New test asserts no warning for projected/meter coordinates.
  • Full test_slope.py suite passes (63 tests).

… path (#2764)

The planar slope path uses coordinate spacing directly as the cell size.
With degree (lat/lon) coordinates and meter elevation values, the result
is wrong by orders of magnitude and nothing told the caller. Wire the
existing warn_if_unit_mismatch helper into the planar branch so a
UserWarning fires when the mismatch is detected.

Add tests asserting the warning is emitted for degree coordinates and not
emitted for projected/meter coordinates. Document the runtime warning in
the slope docstring.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Jun 1, 2026
Copy link
Copy Markdown
Contributor Author

@brendancol brendancol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Review: Warn at runtime on degree coords + meter elevation in slope planar path

Blockers (must fix before merge)

None.

Suggestions (should fix, not blocking)

  • aspect() has the same planar/degree footgun and the same helper would apply there. Out of scope for this PR, but flagging it so it does not get forgotten.

Nits (optional improvements)

  • xrspatial/slope.py: the warning only fires in the planar branch, which is correct since geodesic handles lat/lon properly. Noting it so the asymmetry is on the record as intentional.

What looks good

  • Reuses the existing warn_if_unit_mismatch helper instead of reinventing the check. The helper was tested in isolation but unused; this wires it to a real call site.
  • The warning runs on eager DataArray metadata before backend dispatch, so all four backends behave identically and no array is materialized.
  • Tests cover both directions: the warning fires for degree coords with meter elevation, and stays silent for projected/meter coords. The no-warn test uses simplefilter("error", UserWarning), so any stray warning would fail it too.
  • Docstring Notes section explains the failure mode and points to the fix (reproject or use geodesic).

Checklist

  • Algorithm matches reference: n/a, no algorithm change
  • All implemented backends consistent: yes, check runs pre-dispatch
  • NaN handling: unchanged
  • Edge cases covered by tests: degree and projected cases both covered
  • Dask chunk boundaries: unchanged, warning is metadata-only
  • No premature materialization: confirmed, helper samples metadata only
  • Benchmark: not needed
  • README feature matrix: not needed, no new function
  • Docstrings present and accurate: yes

# Conflicts:
#	xrspatial/tests/test_slope.py
Copy link
Copy Markdown
Contributor Author

@brendancol brendancol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Review: Warn on degree coords + meter elevation in slope planar path

Blockers

None.

Suggestions

None.

Nits

None.

What looks good

  • Reuses the existing warn_if_unit_mismatch helper (utils.py:833) instead of writing fresh detection logic, called at the top of the planar branch (slope.py:402) before the resolution gets used.
  • Scoped to the planar path only. Geodesic is left alone, which is right since it handles lat/lon itself.
  • The check reads coordinate metadata, so it fires once and never triggers a dask compute. That also makes it backend-agnostic.
  • Tests cover both directions: degree coords + meter elevation warns, projected meter coords stay silent under simplefilter("error", UserWarning).
  • The merge with main is clean. The test rasters use evenly spaced coords, so the separately-merged irregular-spacing warning (#2784) doesn't fire and trip the no-warn test. Full test_slope.py passes (67 tests).
  • A Notes section in the slope docstring spells out the failure mode.

Checklist

  • No algorithm change (adds a runtime warning)
  • Backend-agnostic (warning runs on metadata)
  • NaN handling unchanged by this PR
  • Edge cases tested (degree warn, projected no-warn)
  • No neighborhood/dask change
  • Metadata-only check, no materialization
  • No benchmark needed (no compute change)
  • README matrix N/A (no new function, no backend change)
  • Docstring Notes section added

@brendancol brendancol merged commit 2e634b7 into main Jun 2, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance PR touches performance-sensitive code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

slope() planar path does not warn on degree coordinates with meter elevation

1 participant